package com.javakc.mes.basis.mine.controller;

import com.javakc.commonutils.api.APICODE;
import com.javakc.mes.basis.mine.entity.MaintainMine;
import com.javakc.mes.basis.mine.service.MineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/mes/mine")
@Api(tags = "矿井接口")
//@CrossOriginkk
public class MineController {

    @Autowired
    private MineService mineService;

    @GetMapping
    @ApiOperation(value = "查询矿井所有数据")
    public APICODE queryAll() {
        List<MaintainMine> list = mineService.queryAll();
        return APICODE.OK().data("list", list);
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("{pageNo}/{pageSize}")
    public APICODE queryByPage(
            @ApiParam(name = "pageNo", value = "页码", required = true)
            @PathVariable("pageNo") Integer pageNo,
            @ApiParam(name = "pageSize", value = "每页最大条数", required = true)
            @PathVariable("pageSize") Integer pageSize) {
        Page<MaintainMine> page = mineService.queryByPage(pageNo, pageSize);
        List<MaintainMine> list = page.getContent();
        long totalElements = page.getTotalElements();
        return APICODE.OK().data("total", totalElements).data("items", list);
    }

    @ApiOperation(value = "根据id获取矿井信息")
    @GetMapping("{id}")
    public APICODE queryById(@PathVariable(name = "id") Integer id) {
        MaintainMine mine = mineService.queryById(id);
//        System.out.println(mine.getRoadwayList().size());
        return APICODE.OK().data("maintainMine", mine);
    }

    @ApiOperation(value = "新增矿井")
    @PostMapping("saveMine")
    public APICODE saveMine(@RequestBody @Valid MaintainMine mine) {
        mineService.save(mine);
        return APICODE.OK();
    }

    @ApiOperation(value = "根据id修改矿井数据")
    @PostMapping("updateMine")
    public APICODE updateById(@RequestBody @Valid MaintainMine mine) {
        mineService.saveOrUpdate(mine);
        return APICODE.OK();
    }

    @ApiOperation(value = "根据id删除矿井信息")
    @DeleteMapping("{id}")
    public APICODE deleteById(@PathVariable(name = "id") Integer id) {
        mineService.removeById(id);
        return APICODE.OK();
    }

    @ApiOperation(value = "Excel导出", notes = "使用Apache POI技术进行的功能导出")
    @GetMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) {
        mineService.exportExcel(response);
    }

    @ApiOperation(value = "Excel导入", notes = "使用Apache POI技术进行的功能导入")
    @PostMapping("importExcel")
    public void importExcel(MultipartFile file) {
        mineService.importExcel(file);
    }

}
